#include <stdio.h>
#include "sqft_conf.h"
#include <math.h>

int main(int argc, char *argv[]) {
	int bits, i, sts[5];
	int mr2=0, jac=0, cmp1=0, cmp2=0;
	int MAX=10;
	FILE *fp_mr2, *fp_jac, *fp_cmp1, *fp_cmp2;
	bn_t p;
	
	if (core_init() != STS_OK) {
		core_clean();
		return 1;
	}
	
	bn_null(p);
	bn_new(p);
	
	fp_mr2 = fopen("sts/mr2.dat", "w");
	fp_jac = fopen("sts/jac.dat", "w");
	fp_cmp1 = fopen("sts/cmp1.dat", "w");
	fp_cmp2 = fopen("sts/cmp2.dat", "w");
	
	if (argc > 1) {
		for (bits=8; bits<=BN_BITS; bits *= 2) {
		
			mr2=0;
			jac=0;
			cmp1=0;
			cmp2=0;
			
			for (i=0; i<MAX; i++) {
				bn_gen_prime(p, bits);
				bn_is_prime_sqft_sts(p, sts);
				mr2 += sts[0];
				jac += sts[1];
				cmp1 += sts[2];
				cmp2 += sts[3];	
			}
			
			fprintf(fp_mr2, "%d\t%d\n", (int) log2((double) bits), mr2/MAX);
			fprintf(fp_jac, "%d\t%d\n", (int) log2((double) bits), jac/MAX);
			fprintf(fp_cmp1, "%d\t%d\n", (int) log2((double) bits), cmp1/MAX);
			fprintf(fp_cmp2, "%d\t%d\n", (int) log2((double) bits), cmp2/MAX);
			
			printf("\n---- %d BITS ----\n", bits);
			printf("mr2: %d \n", mr2/MAX);
			printf("jac: %d \n", jac/MAX);
			printf("cmp1: %d \n", cmp1/MAX);
			printf("cmp2: %d \n", cmp2/MAX);
		}
		
	}
	else {
		for (bits=8; bits<=BN_BITS; bits *= 2) {
			
			mr2=0;
			jac=0;
			cmp1=0;
			cmp2=0;
			
			for (i=0; i<MAX; i++) {
				bn_gen_prime(p, bits);
				bn_is_prime_sqft_sts(p, sts);
				mr2 += (int)(((float)sts[0]/(float)sts[4])*100);
				jac += (int)(((float)sts[1]/(float)sts[4])*100);
				cmp1 += (int)(((float)sts[2]/(float)sts[4])*100);
				cmp2 += (int)(((float)sts[3]/(float)sts[4])*100);
				
				
				
			}
			
			fprintf(fp_mr2, "%d\t%d\n", (int) log2((double) bits), mr2/MAX);
			fprintf(fp_jac, "%d\t%d\n", (int) log2((double) bits), jac/MAX);
			fprintf(fp_cmp1, "%d\t%d\n", (int) log2((double) bits), cmp1/MAX);
			fprintf(fp_cmp2, "%d\t%d\n", (int) log2((double) bits), cmp2/MAX);
			
			printf("\n---- %d BITS ----\n", bits);
			printf("mr2: %d%% \n", mr2/MAX);
			printf("jac: %d%% \n", jac/MAX);
			printf("cmp1: %d%% \n", cmp1/MAX);
			printf("cmp2: %d%% \n", cmp2/MAX);
		}
	}
	
	bn_free(p);
	fclose(fp_mr2);
	fclose(fp_jac);
	fclose(fp_cmp1);
	fclose(fp_cmp2);
	core_clean();
}
